package com.schoolhelper.model;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import lombok.Data;
import lombok.NoArgsConstructor;

/***
 * 单例类，Redisson类
 */
public class MyRedissonManager {
    static Config config;
    static String address = "redis://likeghee.ltd:6379";
    static String password = "as123456";
    private static RedissonClient redisson;


    private MyRedissonManager() {
        config = new Config();
        config.useSingleServer()
                .setAddress(address)
                .setPassword(password);
        redisson = Redisson.create(config);
        System.out.println("Redisson创建成功");
    }

    /***
     * 懒加载方式获取RedissonClient
     * @return RedissonClient
     */
    public static RedissonClient getRedisson() {
        if (redisson == null) {
            synchronized (MyRedissonManager.class) {
                if (redisson == null) {
                    new MyRedissonManager();
                }
            }
        }
        return redisson;
    }

    public static synchronized void close() {
        if (redisson != null) {
            redisson.shutdown();
            redisson = null;
        }
    }

}